package com.winbomb.simulife.game;

import java.util.Random;

import com.winbomb.simulife.Master;

/**
 * 成长管理器，负责生成下一个月长高了多少
 * 
 * @author winbomb
 * 
 */
public class GrowthManager {

	/** 每个月的最小生长高度,单位为微米 */
	private static final int MIN_GROWTH_HEIGHT = 3240;
	/** 每个月的最大生长高度 */
	private static final int MAX_GROWTH_HEIGHT = 7870;
	/** 每个月平均生长高度 */
	private static final int AVG_GROWTH_HEIGHT = 5550;

	private static final int CM = 10000;

	/** 随机数发生器 */
	private Random rand;

	private Master master;

	public GrowthManager(Master master) {
		this.master = master;
		this.rand = new Random();
	}

	public void grow() {
		master.grow(getHeightGrowth(), getWeightGrowth());
	}

	/**
	 * 获得身高的生长数值，身高数值只能为正数
	 * 
	 * @return
	 */
	private int getHeightGrowth() {

		// 生成[-1,1]的一个随机数
		double r = 5 * rand.nextDouble() - 2.5;

		int delta = 0;
		if (r >= 0) {
			delta = (int) (r * (MAX_GROWTH_HEIGHT - AVG_GROWTH_HEIGHT));
		} else {
			delta = (int) (r * (AVG_GROWTH_HEIGHT - MIN_GROWTH_HEIGHT));
		}

		int grow = AVG_GROWTH_HEIGHT + delta;

		return (grow > 0) ? grow : 0;
	}

	/**
	 * 获得体重的生长数值，体重生长数值可以为负数
	 * 
	 * @return
	 */
	private int getWeightGrowth() {
		return 0;
	}

	public static void main(String args[]) {

		GrowthManager growManager = new GrowthManager(null);

		long sigma = 0;
		int min = Integer.MAX_VALUE;
		int max = 0;

		int r1 = 0; // 1.2 ~ 1.5
		int r2 = 0; // 1.5 ~ 1.6
		int r3 = 0; // 1.6 ~ 1.65
		int r4 = 0; // 1.65 ~ 1.7
		int r5 = 0; // 1.7 ~ 1.75
		int r6 = 0; // 1.75 ~ 1.8
		int r7 = 0; // 1.8 ~ 1.9
		int r8 = 0; // 1.9 ~ 2.0
		int r9 = 0; // 2.0 ~ 2.2
		int r10 = 0; // 2.2 ~ 2.4

		int testCount = 100000;
		for (int i = 0; i < testCount; i++) {
			int height = getAdultHeight(growManager);

			if (height > max) {
				max = height;
			}

			if (height < min) {
				min = height;
			}

			if (height > 120 * CM && height < 150 * CM) {
				r1++;
			} else if (height > 150 * CM && height < 160 * CM) {
				r2++;
			} else if (height > 160 * CM && height < 165 * CM) {
				r3++;
			} else if (height > 165 * CM && height < 170 * CM) {
				r4++;
			} else if (height >= 170 * CM && height < 175 * CM) {
				r5++;
			} else if (height >= 175 * CM && height < 180 * CM) {
				r6++;
			} else if (height >= 180 * CM && height < 190 * CM) {
				r7++;
			} else if (height >= 190 * CM && height < 200 * CM) {
				r8++;
			} else if (height >= 200 * CM && height < 220 * CM) {
				r9++;
			} else if (height >= 220 * CM && height < 240 * CM) {
				r10++;
			}

			sigma += height;
		}

		System.out.println("Avg: " + sigma / testCount);
		System.out.println("Max: " + max);
		System.out.println("Min: " + min);
		System.out.println("1.2~1.5: " + r1 + " " + (float) r1 * 100 / testCount + " %");
		System.out.println("1.5~1.6: " + r2 + " " + (float) r2 * 100 / testCount + " %");
		System.out.println("1.6~1.65: " + r3 + " " + (float) r3 * 100 / testCount + " %");
		System.out.println("1.65~1.7: " + r4 + " " + (float) r4 * 100 / testCount + " %");
		System.out.println("1.7~1.75: " + r5 + " " + (float) r5 * 100 / testCount + " %");
		System.out.println("1.75~1.8: " + r6 + " " + (float) r6 * 100 / testCount + " %");
		System.out.println("1.8~1.9: " + r7 + " " + (float) r7 * 100 / testCount + " %");
		System.out.println("1.9~2.0: " + r8 + " " + (float) r8 * 100 / testCount + " %");
		System.out.println("2.0~2.2: " + r9 + " " + (float) r9 * 100 / testCount + " %");
		System.out.println("2.2~2.4: " + r10 + " " + (float) r10 * 100 / testCount + " %");

	}

	private static int getAdultHeight(GrowthManager growManager) {

		int initHeight = 520000;

		for (int i = 0; i < 216; i++) {
			initHeight += growManager.getHeightGrowth();
		}

		return initHeight;
	}
}
